﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using AutoStockTrade.Interface;
using System.Xml;

namespace AutoStockTrade.Presenter
{
    public class SendDataPrensenter : IStockOperation
    {
        XmlDocument xmlDoc = new XmlDocument();
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern IntPtr GetForegroundWindow();   //WINAPI 获取当前活动窗体的句柄

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

        const int WM_KEYDOWN = 0x0100;//按键按下
        const int WM_KEYUP = 0x0101;//按键放开
        const int WM_SETTEXT = 0x000C;//发送文本
        const int WM_CLICK = 0x00F5;//鼠标点击
        const int WM_SETFOCUS = 0x0007;//设置焦点参数

        public void buy_Stock(string Code, string Price, string Qty, int time)
        {
            //IntPtr hWnd1 = FindWindow(null, "网上股票交易系统5.0");
            //SetForegroundWindow(hWnd1);
            //IntPtr hWnd2 = FindWindowEx(hWnd1, IntPtr.Zero, "AfxMDIFrame42s", null);
            //IntPtr hWnd3 = FindWindowEx(hWnd1, IntPtr.Zero, "#32770", null);
            //SendKeys.SendWait("{F1}");
            SendKeys.SendWait(Code);
            Wait();
            SendKeys.SendWait("{TAB}");
            Wait();
            SendKeys.SendWait(Price);
            Wait();
            SendKeys.SendWait("{TAB}");
            Wait();
            SendKeys.SendWait(Qty);
            Wait();
            SendKeys.SendWait("{ENTER}");
            Wait();
            SendKeys.SendWait("{ENTER}");
            Wait();
            SendKeys.SendWait("{ENTER}");
            Wait();
            xmlDoc.Load("strickstock.xml");


            XmlNode root = xmlDoc.SelectSingleNode("stocklist");//查找<stocklist>
            XmlElement xe1 = xmlDoc.CreateElement("stock");//创建一个<stock>节点

            XmlElement xesub1 = xmlDoc.CreateElement("Code");//添加一个名字为Code的子节点
            xesub1.InnerText =Code;//设置文本
            xe1.AppendChild(xesub1);//把Code添加到<stock>节点中

            XmlElement xesub2 = xmlDoc.CreateElement("Price");
            xesub2.InnerText = Price;
            xe1.AppendChild(xesub2);

            XmlElement xesub3 = xmlDoc.CreateElement("Qty");
            xesub3.InnerText = Qty;
            xe1.AppendChild(xesub3);

            XmlElement xesub4 = xmlDoc.CreateElement("Operation");
            xesub4.InnerText = "buy";
            xe1.AppendChild(xesub4);

            XmlElement xesub5 = xmlDoc.CreateElement("Date");
            xesub5.InnerText = DateTime.Now.ToString("yyyy/dd/MM");
            xe1.AppendChild(xesub5);

            root.AppendChild(xe1);//把stock添加到<stocklist>根节点中
            xmlDoc.Save("strickstock.xml");

        }

        private void Wait()
        {
            Thread.Sleep(1000);
        }

        public void sell_Stock(string Code, string Price, string Qty, int time)
        {
            //IntPtr hWnd1 = FindWindow(null, "网上股票交易系统5.0");
            //SetForegroundWindow(hWnd1);
            ////IntPtr hWnd2 = FindWindowEx(hWnd1, IntPtr.Zero, "AfxMDIFrame42s", null);
            ////IntPtr hWnd3 = FindWindowEx(hWnd1, IntPtr.Zero, "#32770", null);
            //SendKeys.SendWait("{F2}");
            SendKeys.SendWait(Code);
            Wait();
            SendKeys.SendWait("{TAB}");
            Wait();
            SendKeys.SendWait(Price);
            Wait();
            SendKeys.SendWait("{TAB}");
            Wait();
            SendKeys.SendWait(Qty);
            Wait();
            SendKeys.SendWait("{TAB}");
            Wait();
            SendKeys.SendWait("{ENTER}");
            Wait();
            SendKeys.SendWait("{ENTER}");
            Wait();
            SendKeys.SendWait("{ENTER}");
            Wait();
            //SendKeys.SendWait("{TAB}");
            //Wait();
            XmlNode root = xmlDoc.SelectSingleNode("stocklist");//查找<stocklist>
            XmlElement xe1 = xmlDoc.CreateElement("stock");//创建一个<stock>节点

            XmlElement xesub1 = xmlDoc.CreateElement("Code");//添加一个名字为Code的子节点
            xesub1.InnerText = Code;//设置文本
            xe1.AppendChild(xesub1);//把Code添加到<stock>节点中

            XmlElement xesub2 = xmlDoc.CreateElement("Price");
            xesub2.InnerText = Price;
            xe1.AppendChild(xesub2);

            XmlElement xesub3 = xmlDoc.CreateElement("Qty");
            xesub3.InnerText = Qty;
            xe1.AppendChild(xesub3);

            XmlElement xesub4 = xmlDoc.CreateElement("Operation");
            xesub4.InnerText = "sell";
            xe1.AppendChild(xesub4);

            XmlElement xesub5 = xmlDoc.CreateElement("Date");
            xesub5.InnerText = DateTime.Now.ToString("YYYY/DD/MM");
            xe1.AppendChild(xesub5);

            root.AppendChild(xe1);//把stock添加到<stocklist>根节点中
            xmlDoc.Save("strickstock.xml");
        }

        private bool _isBusy;
        public bool IsBusy
        {
            get
            {
                return _isBusy; 
            }
            set
            {
                _isBusy = value;
            }
        }

    }
}
